;;;; lserver.asd

(defclass c-program (module)
  ((install-dir :initarg :install-dir :initform nil))
  (:documentation
   "This ASDF component represent a c program."))

(defmethod output-files ((op compile-op) (component c-program))
  (let   ((exefile (make-pathname :name (component-name component)
                                  :type #+linux nil #+winows "exe" #+darwin nil
                                  :defaults (component-pathname (component-parent component)))))
    (format *standard-output* "~%; Output program as ~A ~&" (apply-output-translations exefile))
    (list exefile)))

(defmethod perform ((op prepare-op) (component c-program))
  (values))

(defmethod perform ((op compile-op) (component c-program))
  (let ((cd-dir (format nil "cd ~A; " (component-pathname (component-parent component)) 'string)))
    (with-slots (install-dir) component
      (let ((cmds (list
		   (format nil "make ~A" (component-name component))
		   (format nil "cp -f ~A ~A" (component-name component)
			   (car (output-files 'compile-op component)))
		   (format nil "mkdir -p ~A; ln -sf ~A ~A "
			   install-dir
			   (car (output-files 'compile-op component))
			   install-dir)
		   (format nil "make clean"))))
	(format *standard-output* "~%; Making program ~A ~&" component)
	(dolist (cmd cmds)
	  (uiop:run-program (concatenate 'string cd-dir cmd)
			    :output *standard-output*
			    :error-output *standard-output*))))))
  
(defmethod perform ((op load-op) (component c-program))
  nil)

(asdf:defsystem #:lserver-cli
  :description "A C program sending command to a lisp server"
  :author "Stanislav Kondratyev <kondratjevsk@gmail.com>"
  :license "CC0"
  :depends-on ()
  :components (
	       (:c-program "lcli"
			   :install-dir "~/.lserver/bin/"
			   :components ((:static-file "Makefile")
					(:static-file "interpreter.h")
					(:static-file "interpreter.c")
					(:static-file "lcli.c")))))



